package com.xincongjun.media.update.time;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

import static com.xincongjun.media.utils.DateTimeUtil.generateRandomTimeString;
import static com.xincongjun.media.utils.PowerShellUtil.usePowerShellUpdateTime;
import static com.xincongjun.media.utils.ExifToolUtil.useExifToolUpdateTime;
import static com.xincongjun.media.utils.FileUtil.getFileExtension;
import static com.xincongjun.media.utils.FileUtil.getRealFileType;
import static com.xincongjun.media.utils.POIUtil.usePOIUpdateTime;


public class UpdateTime {

    private static final Logger logger = LoggerFactory.getLogger(UpdateTime.class);

    //    creationTime, lastWriteTime, lastAccessTime 的时间格式：yyyy-MM-dd HH:mm:ss
    public static void updateFileTime(String directoryPath, String creationTime, String lastWriteTime, String lastAccessTime, boolean isUseCustom, String targetTimeStr, String startTimeStr, String endTimeStr, boolean isUseRandomTime) {
        File dir = new File(directoryPath);
        if (!dir.exists() || !dir.isDirectory()) {
            logger.error("无效路径：{}", directoryPath);
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            logger.error("路径下不存在文件: {}", directoryPath);
            return;
        }

        for (File file : files) {
            if (file.isFile()) {
                updateFileTimeOfProcessFile(file, creationTime, lastWriteTime, lastAccessTime, isUseCustom, targetTimeStr, startTimeStr, endTimeStr, isUseRandomTime);
            }
        }

        logger.info("所有文件处理完成！");
    }

    private static void updateFileTimeOfProcessFile(File file, String creationTime, String lastWriteTime, String lastAccessTime, boolean isUseCustom, String targetTimeStr, String startTimeStr, String endTimeStr, boolean isUseRandomTime) {
        String filePath = file.getAbsolutePath();

        try {
            if (isUseCustom) {
                // 使用 PowerShell 更新时间
                usePowerShellUpdateTime(filePath, creationTime, lastWriteTime, lastAccessTime);
                return;
            }

            if (isUseRandomTime) {
                targetTimeStr = generateRandomTimeString(startTimeStr, endTimeStr);
            }

            // 使用 PowerShell 更新时间
            usePowerShellUpdateTime(filePath, targetTimeStr);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    //    creationTime, lastWriteTime, lastAccessTime 的时间格式：yyyy-MM-dd HH:mm:ss
    public static void updateFileTime(String directoryPath, String creationTime, String lastWriteTime, String lastAccessTime) {
        updateFileTime(directoryPath, creationTime, lastWriteTime, lastAccessTime, true, null, null, null, false);
    }

    //    targetTimeStr, startTimeStr, endTimeStr 的时间格式：yyyy-MM-dd HH:mm:ss
    public static void updateFileTime(String directoryPath, String targetTimeStr, String startTimeStr, String endTimeStr, boolean isUseRandomTime) {
        updateFileTime(directoryPath, null, null, null, false, targetTimeStr, startTimeStr, endTimeStr, isUseRandomTime);
    }

    //    targetTimeStr, startTimeStr, endTimeStr 的时间格式：yyyy:MM:dd HH:mm:ss
    public static void updateMediaFileTime(String directoryPath, String targetTimeStr, String startTimeStr, String endTimeStr, String timeZoneType, boolean isUseRandomTime, List<String> timeFields) {
        File dir = new File(directoryPath);
        if (!dir.exists() || !dir.isDirectory()) {
            logger.error("无效路径: {}", directoryPath);
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            logger.error("路径下不存在文件: {}", directoryPath);
            return;
        }

        for (File file : files) {
            if (file.isFile()) {
                if (isUseRandomTime) {
                    targetTimeStr = generateRandomTimeString(startTimeStr, endTimeStr);
                }
                updateMediaFileTimeProcessFile(file, targetTimeStr, timeZoneType, timeFields);
            }
        }

        logger.info("所有文件处理完成！");
    }

    private static void updateMediaFileTimeProcessFile(File file, String timeString, String timeZoneType, List<String> timeFields) {
        String filePath = file.getAbsolutePath();
        String fileName = file.getName();
        String fileExtension = getFileExtension(fileName);

        try {
            String realFileType = getRealFileType(filePath);
            // 若文件后缀名和 exiftool 检测的 FileTypeExtension 一致，则执行时间修改
            if (!realFileType.equals(fileExtension)) {
                logger.error("文件后缀名与 FileTypeExtension 字段的值不一致：{}", filePath);
                return;
            }
            // 使用 Exif 更新时间
            useExifToolUpdateTime(filePath, timeString, timeZoneType, timeFields);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //    targetTimeStr, startTimeStr, endTimeStr 的时间格式：yyyy:MM:dd HH:mm:ss
    public static void updateFileTimeAndMediaFileTime(String directoryPath, String targetTimeStr, String startTimeStr, String endTimeStr, String timeZoneType, boolean isUseRandomTime, List<String> timeFields) {
        File dir = new File(directoryPath);
        if (!dir.exists() || !dir.isDirectory()) {
            logger.error("无效路径: {}", directoryPath);
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            logger.error("路径下不存在文件: {}", directoryPath);
            return;
        }

        for (File file : files) {
            if (file.isFile()) {
                if (isUseRandomTime) {
                    targetTimeStr = generateRandomTimeString(startTimeStr, endTimeStr);
                }
                updateFileTimeAndMediaFileTimeProcessFile(file, targetTimeStr, timeZoneType, timeFields);
            }
        }

        logger.info("所有文件处理完成！");
    }

    private static void updateFileTimeAndMediaFileTimeProcessFile(File file, String timeString, String timeZoneType, List<String> timeFields) {
        String filePath = file.getAbsolutePath();
        String fileName = file.getName();
        String fileExtension = getFileExtension(fileName);

        try {
            String realFileType = getRealFileType(filePath);
            if (!fileExtension.equals(realFileType)) {
                logger.error("文件后缀名与 realFileType 不一致: {}", filePath);
                return;
            }

            // 使用 Exif 更新时间
            useExifToolUpdateTime(filePath, timeString, timeZoneType, timeFields);

            // 使用 PowerShell 更新时间
            usePowerShellUpdateTime(filePath, timeString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateMicrosoftOfficeFileTime(String directoryPath, String targetTimeStr, String startTimeStr, String endTimeStr, boolean isUseRandomTime) {
        File dir = new File(directoryPath);
        if (!dir.exists() || !dir.isDirectory()) {
            logger.error("无效路径: {}", directoryPath);
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            logger.error("路径下不存在文件: {}", directoryPath);
            return;
        }

        for (File file : files) {
            if (file.isFile()) {
                if (isUseRandomTime) {
                    targetTimeStr = generateRandomTimeString(startTimeStr, endTimeStr);
                }
                updateMicrosoftOfficeFileTimeProcessFile(file, targetTimeStr);
            }
        }

        logger.info("所有文件处理完成！");
    }

    private static void updateMicrosoftOfficeFileTimeProcessFile(File file, String timeString){
        String filePath = file.getAbsolutePath();

        try {
            usePOIUpdateTime(filePath, timeString);

            usePowerShellUpdateTime(filePath, timeString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }







}
